/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.gulangyu.manage.mvc;

import com.mycompany.yourtrade.common.DerongVariables;
import com.mycompany.yourtrade.common.UpYunService;
import com.mycompany.yourtrade.model.AjaxResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
public class FileUploadController {

    private static final Logger logger = LoggerFactory
            .getLogger(FileUploadController.class);

    @Autowired
    private UpYunService upYunService;

    @Autowired
    private DerongVariables derongVariables;

    @RequestMapping(value = "/uploadFile", method = RequestMethod.GET)
    public String uploadFileHandler() {
        return "upload";
    }

    @RequestMapping(value = "/uploadMultipleFile", method = RequestMethod.GET)
    public String uploadMultipleFileHandler() {

        return "uploadMultiple";
    }

    @RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
    public @ResponseBody
    String uploadFileHandler(@RequestParam("name") String name,
            @RequestParam("file") MultipartFile file) {

        if (!file.isEmpty()) {
            try {
                String newName = this.upYunService.Test(file);
                return "Cdn is  http://derong.b0.upaiyun.com/, you successfully uploaded file=" + newName;
            } catch (Exception e) {
                return "You failed to upload " + name + " => " + e.getMessage();
            }
        } else {
            return "You failed to upload " + name
                    + " because the file was empty.";
        }
    }

    @RequestMapping(value = "/uploadAjaxFile", method = RequestMethod.POST)
    public @ResponseBody
    AjaxResponse uploadAjaxFileHandler(@RequestParam("name") String name,
            @RequestParam("file") MultipartFile file) {

        if (!file.isEmpty()) {
            try {
                String newName = this.upYunService.Test(file);
                return new AjaxResponse(true, derongVariables.getDomain() + newName);
            } catch (Exception e) {
                logger.error("You failed to upload,reason is {}", e.getMessage());
                return new AjaxResponse(false);
            }
        } else {
            logger.error("You failed to upload {},because the file was empty.", name);
            return new AjaxResponse(false);
        }
    }

    @RequestMapping(value = "/uploadMultipleFile", method = RequestMethod.POST)
    public @ResponseBody
    String uploadMultipleFileHandler(@RequestParam("name") String[] names,
            @RequestParam("file") MultipartFile[] files) {

        String message = "";
        for (int i = 0; i < files.length; i++) {
            MultipartFile file = files[i];
            String name = names[i];
            try {

                message = message + "You successfully uploaded file=" + name
                        + "<br />";
            } catch (Exception e) {
                return "You failed to upload " + name + " => " + e.getMessage();
            }
        }
        return message;
    }
}
